<template>
  <div class="openBank">
    <el-row style="padding: 10px">
      <el-col :span="2" style="line-height: 32px;">卡/账号：</el-col>
      <el-col :span="18">
        <el-input size="small" v-model="bankNo" clearable placeholder="请输入卡/账号"></el-input>
      </el-col>
      <el-col :span="2" :offset="1">
        <el-button type="primary" @click="openBankQuery">查询</el-button>
      </el-col>
    </el-row>
    <el-row>
      <SimpleData :simpleData="openBankData" :option="openBankOption" :buttonSelf="openBankArray"
                  @selfButton="openBankButton"></SimpleData>
    </el-row>
  </div>
</template>

<script>
  import SimpleData from '@/components/common/SimpleData'
  import {queryLoss, sendContentMessage} from '@/api/bankBiz/loss/loss'

  export default {
    name: 'tableView',
    components: {
      SimpleData
    },
    data() {
      return {
        bankNo: '',
        openBankData: {},
        openBankOption: {
          column: [
            {
              'prop': 'openBankName',
              'label': '开户行名称',
              'span': 12
            },
            {
              'prop': 'address',
              'label': '开户行地址',
              'span': 12
            },
            {
              'prop': 'largeNum',
              'label': '大额行号',
              'span': 12
            },
            {
              'prop': 'phone',
              'label': '开户行电话',
              'span': 12
            },
            {
              'prop': 'telReservation',
              'label': '预留手机号',
              'span': 12,
              'mask': 'mobilePhone'
            }
          ]
        },
        openBankArray: [
          {
            label: '短信发送',
            prop: 'personal-openBankData-msgSend'
          }
        ],
        query: {
          namespace: 'PublicInfo',
          key: 'openBankInquiry',
          query: {
            accountNo: '1'
          }
        }
      }
    },
    mounted() {
    },
    methods: {
      // 短信发送
      openBankButton(prop) {
        if ('personal-openBankData-msgSend' == prop) {
          if (this.$store.getters.GET_OFFLINESTATUS) {
            window.globalUiKit.notify('提示', '离线状态不能发送短信！', 'error')
            return
          } else if (Object.keys(this.openBankData).length == 0) {
            window.globalUiKit.notify('提示', '短信发送失败！', 'error')
            return
          }
          else if (!Constants.regExpType.mobilePhone.test(Constants.Model.uid)) {
            window.globalUiKit.notify('提示', '请输入正确格式的手机号码！', 'error')
            return
          }
          let params = {
            'content': '您所查询的尾号后四位为' + this.bankNo.substring(this.bankNo.length - 4) +
            '的账户开户机构为：' + this.openBankData.openBankName +
            '，地址：' + this.openBankData.address +
            '，电话：' + this.openBankData.phone +
            '，支付系统行号：' + this.openBankData.largeNum + '。',
            'phone': Constants.Model.uid,
            'agentName': Constants.Agent.agentName,
            'agentId': Constants.Agent.agentId,
            'smsType': '综合业务'
          }
          sendContentMessage(params).then(res => {
            if (200 == res.data.code) {
              console.log('send message success')
              window.globalUiKit.notify('提示', '短信发送成功！', 'success')
            } else {
              window.globalUiKit.alert('短信发送失败', '失败原因：' + res.data.message + '；入参：' + JSON.stringify(params))
            }
          }).catch(error => {
            console.log(error)
            window.globalUiKit.alert('短信发送失败', '失败原因：' + error + '；入参：' + JSON.stringify(params))
          })
        }
      },
      _queryCustInfo() {
        this.query.query.accountNo = this.bankNo
        queryLoss(this.query).then((res) => {
          console.log('response data is: ' + JSON.stringify(res.data))
          if (200 == res.data.code) {
            this.openBankData = res.data.data
          } else {
            this.openBankData = {}
            window.globalUiKit.notify('提示', res.data.message, 'error')
          }
        }).catch((err) => {
          console.log(err)
        })
      },
      // 开户行查询
      openBankQuery() {
        this.openBankData = {}
        this.validateBank(this.bankNo)
      },
      validateBank(value) {
        const bankReg = /^\d{1,}$/
        if (value) {
          if (bankReg.test(value)) {
            // 账号
            if (value.indexOf('070667') != -1) {
              // if (value.charAt(11) != '1') {
              //   window.globalUiKit.notify('提示', '请输入对私账号！', 'error')
              //   return
              // } else if (value.length != 22) {
              //   window.globalUiKit.notify('提示', '请输入22位的对私账号！', 'error')
              //   return
              // }
              if (value.length != 22) {
                window.globalUiKit.notify('提示', '请输入22位的账号！', 'error')
                return
              } else {
                this._queryCustInfo()
              }
            }
            // 卡号
            else if (value.indexOf('62311003') != -1 ||
              value.indexOf('623503001') != -1 ||
              value.indexOf('62299502') != -1 ||
              value.indexOf('62311005') != -1 ||
              value.indexOf('62311007') != -1 ||
              value.indexOf('62264802') != -1 ||
              value.indexOf('62311008') != -1 ||
              value.indexOf('62311006') != -1 ||
              value.indexOf('62264801') != -1 ||
              value.indexOf('62248800') != -1 ||
              value.indexOf('62299501') != -1 ||
              value.indexOf('62248802') != -1 ||
              value.indexOf('62248803') != -1 ||
              value.indexOf('62248810') != -1 ||
              value.indexOf('62248811') != -1 ||
              value.indexOf('62248820') != -1 ||
              value.indexOf('62248880') != -1 ||
              value.indexOf('62248888') != -1 ||
              value.indexOf('62264800') != -1 ||
              value.indexOf('62268811') != -1 ||
              value.indexOf('62268888') != -1 ||
              value.indexOf('62824899') != -1 ||
              value.indexOf('62248801') != -1 ||
              value.indexOf('62824866') != -1 ||
              value.indexOf('62311000') != -1 ||
              value.indexOf('62311001') != -1 ||
              value.indexOf('62311009') != -1 ||
              value.indexOf('62311002') != -1) {
              if (value.length != 16) {
                window.globalUiKit.notify('提示', '请输入16位的卡号！', 'error')
                return
              } else {
                this._queryCustInfo()
              }
            } else {
              window.globalUiKit.notify('提示', '请输入正确的卡/账号！', 'error')
              return
            }
          } else {
            window.globalUiKit.notify('提示', '卡/账号必须为纯数字！', 'error')
            return
          }
        } else {
          window.globalUiKit.notify('提示', '请输入卡/账号！', 'error')
          return
        }
      }

    }
  }
</script>

<style>
</style>
